home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / amitcp / netinet / tcp_timer.c < prev    next >
C/C++ Source or Header  |  1993-08-12  |  10KB  |  352 lines

  1. RCS_ID_C="$Id: tcp_timer.c,v 1.8 1993/06/04 11:16:15 jraja Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * HISTORY
  8.  * $Log: tcp_timer.c,v $
  9.  * Revision 1.8  1993/06/04  11:16:15  jraja
  10.  * Fixes for first public release.
  11.  *
  12.  * Revision 1.7  1993/05/17  00:16:44  ppessi
  13.  * Changed RCS version. Added rcsid.
  14.  *
  15.  * Revision 1.6  1993/04/13  22:15:52  jraja
  16.  * Removed casts added earlier, because SAS did not like them :-(
  17.  *
  18.  * Revision 1.5  93/04/11  22:24:30  22:24:30  jraja (Jarno Tapio Rajahalme)
  19.  * Added cast from short to u_short in comparison with an u_short.
  20.  * 
  21.  * Revision 1.4  93/04/05  19:06:40  19:06:40  jraja (Jarno Tapio Rajahalme)
  22.  * Changed storage of the spl functions  return values to type spl_t.
  23.  * Added include for conf.h to every .c file.
  24.  * 
  25.  * Revision 1.3  93/03/13  17:14:26  17:14:26  ppessi (Pekka Pessi)
  26.  * Fixed bugs with variable initialization.
  27.  * 
  28.  * Revision 1.2  93/02/26  09:54:01  09:54:01  jraja (Jarno Tapio Rajahalme)
  29.  * Made this compile with ANSI C (added prototypes).
  30.  * 
  31.  * Revision 1.1  92/11/17  16:30:57  16:30:57  jraja (Jarno Tapio Rajahalme)
  32.  * Initial revision
  33.  * 
  34.  */
  35.  
  36. /*
  37.  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
  38.  * All rights reserved.
  39.  *
  40.  * Redistribution and use in source and binary forms, with or without
  41.  * modification, are permitted provided that the following conditions
  42.  * are met:
  43.  * 1. Redistributions of source code must retain the above copyright
  44.  *    notice, this list of conditions and the following disclaimer.
  45.  * 2. Redistributions in binary form must reproduce the above copyright
  46.  *    notice, this list of conditions and the following disclaimer in the
  47.  *    documentation and/or other materials provided with the distribution.
  48.  * 3. All advertising materials mentioning features or use of this software
  49.  *    must display the following acknowledgement:
  50.  *    This product includes software developed by the University of
  51.  *    California, Berkeley and its contributors.
  52.  * 4. Neither the name of the University nor the names of its contributors
  53.  *    may be used to endorse or promote products derived from this software
  54.  *    without specific prior written permission.
  55.  *
  56.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  57.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  59.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  60.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  61.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  62.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  63.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  64.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  66.  * SUCH DAMAGE.
  67.  *
  68.  *    @(#)tcp_timer.c    7.18 (Berkeley) 6/28/90
  69.  */
  70.  
  71. #include <conf.h>
  72.  
  73. #include <sys/param.h>
  74. #include <sys/systm.h>
  75. #include <sys/malloc.h>
  76. #include <sys/mbuf.h>
  77. #include <sys/socket.h>
  78. #include <sys/socketvar.h>
  79. #include <sys/protosw.h>
  80. #include <sys/errno.h>
  81. #include <sys/synch.h>
  82.  
  83. #include <net/if.h>
  84. #include <net/route.h>
  85.  
  86. #include <netinet/in.h>
  87. #include <netinet/in_systm.h>
  88. #include <netinet/ip.h>
  89. #include <netinet/in_pcb.h>
  90. #include <netinet/ip_var.h>
  91. #include <netinet/tcp.h>
  92. #include <netinet/tcp_fsm.h>
  93. #include <netinet/tcp_seq.h>
  94. #include <netinet/tcp_timer.h>
  95. #include <netinet/tcp_var.h>
  96. #include <netinet/tcpip.h>
  97.  
  98. #include <netinet/tcp_timer_protos.h>
  99. #include <netinet/tcp_subr_protos.h>
  100. #include <netinet/tcp_output_protos.h>
  101. #include <netinet/tcp_usrreq_protos.h>
  102. #include <netinet/in_pcb_protos.h>
  103.  
  104. int    tcp_keepidle = TCPTV_KEEP_IDLE;
  105. int    tcp_keepintvl = TCPTV_KEEPINTVL;
  106. int    tcp_maxidle = 0;
  107. /*
  108.  * Fast timeout routine for processing delayed acks
  109.  */
  110. void
  111. tcp_fasttimo()
  112. {
  113.     register struct inpcb *inp;
  114.     register struct tcpcb *tp;
  115.     spl_t s = splnet();
  116.  
  117.     inp = tcb.inp_next;
  118.     if (inp)
  119.     for (; inp != &tcb; inp = inp->inp_next)
  120.         if ((tp = (struct tcpcb *)inp->inp_ppcb) &&
  121.             (tp->t_flags & TF_DELACK)) {
  122.             tp->t_flags &= ~TF_DELACK;
  123.             tp->t_flags |= TF_ACKNOW;
  124.             tcpstat.tcps_delack++;
  125.             (void) tcp_output(tp);
  126.         }
  127.     splx(s);
  128. }
  129.  
  130. /*
  131.  * Tcp protocol timeout routine called every 500 ms.
  132.  * Updates the timers in all active tcb's and
  133.  * causes finite state machine actions if timers expire.
  134.  */
  135. void
  136. tcp_slowtimo()
  137. {
  138.     register struct inpcb *ip, *ipnxt;
  139.     register struct tcpcb *tp;
  140.     spl_t s = splnet();
  141.     register int i;
  142.  
  143.     tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl;
  144.     /*
  145.      * Search through tcb's and update active timers.
  146.      */
  147.     ip = tcb.inp_next;
  148.     if (ip == 0) {
  149.         splx(s);
  150.         return;
  151.     }
  152.     for (; ip != &tcb; ip = ipnxt) {
  153.         ipnxt = ip->inp_next;
  154.         tp = intotcpcb(ip);
  155.         if (tp == 0)
  156.             continue;
  157.         for (i = 0; i < TCPT_NTIMERS; i++) {
  158.             if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
  159.                 (void) tcp_usrreq(tp->t_inpcb->inp_socket,
  160.                     PRU_SLOWTIMO, (struct mbuf *)0,
  161.                     (struct mbuf *)i, (struct mbuf *)0);
  162.                 if (ipnxt->inp_prev != ip)
  163.                     goto tpgone;
  164.             }
  165.         }
  166.         tp->t_idle++;
  167.         if (tp->t_rtt)
  168.             tp->t_rtt++;
  169. tpgone:
  170.         ;
  171.     }
  172.     tcp_iss += TCP_ISSINCR/PR_SLOWHZ;        /* increment iss */
  173. #if TCP_COMPAT_42
  174.     if ((int)tcp_iss < 0)
  175.         tcp_iss = 0;                /* XXX */
  176. #endif
  177.     splx(s);
  178. }
  179.  
  180. /*
  181.  * Cancel all timers for TCP tp.
  182.  */
  183. void
  184. tcp_canceltimers(tp)
  185.     struct tcpcb *tp;
  186. {
  187.     register int i;
  188.  
  189.     for (i = 0; i < TCPT_NTIMERS; i++)
  190.         tp->t_timer[i] = 0;
  191. }
  192.  
  193. int    tcp_backoff[TCP_MAXRXTSHIFT + 1] =
  194.     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
  195.  
  196. /*
  197.  * TCP timer processing.
  198.  */
  199. struct tcpcb *
  200. tcp_timers(tp, timer)
  201.     register struct tcpcb *tp;
  202.     int timer;
  203. {
  204.     register int rexmt;
  205.  
  206.     switch (timer) {
  207.  
  208.     /*
  209.      * 2 MSL timeout in shutdown went off.  If we're closed but
  210.      * still waiting for peer to close and connection has been idle
  211.      * too long, or if 2MSL time is up from TIME_WAIT, delete connection
  212.      * control block.  Otherwise, check again in a bit.
  213.      */
  214.     case TCPT_2MSL:
  215.         if (tp->t_state != TCPS_TIME_WAIT &&
  216.             tp->t_idle <= tcp_maxidle)
  217.             tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
  218.         else
  219.             tp = tcp_close(tp);
  220.         break;
  221.  
  222.     /*
  223.      * Retransmission timer went off.  Message has not
  224.      * been acked within retransmit interval.  Back off
  225.      * to a longer retransmit interval and retransmit one segment.
  226.      */
  227.     case TCPT_REXMT:
  228.         if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
  229.             tp->t_rxtshift = TCP_MAXRXTSHIFT;
  230.             tcpstat.tcps_timeoutdrop++;
  231.             tp = tcp_drop(tp, tp->t_softerror ?
  232.                 tp->t_softerror : ETIMEDOUT);
  233.             break;
  234.         }
  235.         tcpstat.tcps_rexmttimeo++;
  236.         rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
  237.         TCPT_RANGESET(tp->t_rxtcur, rexmt,
  238.             tp->t_rttmin, TCPTV_REXMTMAX);
  239.         tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
  240.         /*
  241.          * If losing, let the lower level know and try for
  242.          * a better route.  Also, if we backed off this far,
  243.          * our srtt estimate is probably bogus.  Clobber it
  244.          * so we'll take the next rtt measurement as our srtt;
  245.          * move the current srtt into rttvar to keep the current
  246.          * retransmit times until then.
  247.          */
  248.         if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
  249.             in_losing(tp->t_inpcb);
  250.             tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
  251.             tp->t_srtt = 0;
  252.         }
  253.         tp->snd_nxt = tp->snd_una;
  254.         /*
  255.          * If timing a segment in this window, stop the timer.
  256.          */
  257.         tp->t_rtt = 0;
  258.         /*
  259.          * Close the congestion window down to one segment
  260.          * (we'll open it by one segment for each ack we get).
  261.          * Since we probably have a window's worth of unacked
  262.          * data accumulated, this "slow start" keeps us from
  263.          * dumping all that data as back-to-back packets (which
  264.          * might overwhelm an intermediate gateway).
  265.          *
  266.          * There are two phases to the opening: Initially we
  267.          * open by one mss on each ack.  This makes the window
  268.          * size increase exponentially with time.  If the
  269.          * window is larger than the path can handle, this
  270.          * exponential growth results in dropped packet(s)
  271.          * almost immediately.  To get more time between 
  272.          * drops but still "push" the network to take advantage
  273.          * of improving conditions, we switch from exponential
  274.          * to linear window opening at some threshhold size.
  275.          * For a threshhold, we use half the current window
  276.          * size, truncated to a multiple of the mss.
  277.          *
  278.          * (the minimum cwnd that will give us exponential
  279.          * growth is 2 mss.  We don't allow the threshhold
  280.          * to go below this.)
  281.          */
  282.         {
  283.         u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
  284.         if (win < 2)
  285.             win = 2;
  286.         tp->snd_cwnd = tp->t_maxseg;
  287.         tp->snd_ssthresh = win * tp->t_maxseg;
  288.         tp->t_dupacks = 0;
  289.         }
  290.         (void) tcp_output(tp);
  291.         break;
  292.  
  293.     /*
  294.      * Persistance timer into zero window.
  295.      * Force a byte to be output, if possible.
  296.      */
  297.     case TCPT_PERSIST:
  298.         tcpstat.tcps_persisttimeo++;
  299.         tcp_setpersist(tp);
  300.         tp->t_force = 1;
  301.         (void) tcp_output(tp);
  302.         tp->t_force = 0;
  303.         break;
  304.  
  305.     /*
  306.      * Keep-alive timer went off; send something
  307.      * or drop connection if idle for too long.
  308.      */
  309.     case TCPT_KEEP:
  310.         tcpstat.tcps_keeptimeo++;
  311.         if (tp->t_state < TCPS_ESTABLISHED)
  312.             goto dropit;
  313.         if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&
  314.             tp->t_state <= TCPS_CLOSE_WAIT) {
  315.                 if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
  316.                 goto dropit;
  317.             /*
  318.              * Send a packet designed to force a response
  319.              * if the peer is up and reachable:
  320.              * either an ACK if the connection is still alive,
  321.              * or an RST if the peer has closed the connection
  322.              * due to timeout or reboot.
  323.              * Using sequence number tp->snd_una-1
  324.              * causes the transmitted zero-length segment
  325.              * to lie outside the receive window;
  326.              * by the protocol spec, this requires the
  327.              * correspondent TCP to respond.
  328.              */
  329.             tcpstat.tcps_keepprobe++;
  330. #if TCP_COMPAT_42
  331.             /*
  332.              * The keepalive packet must have nonzero length
  333.              * to get a 4.2 host to respond.
  334.              */
  335.             tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
  336.                 tp->rcv_nxt - 1, tp->snd_una - 1, 0);
  337. #else
  338.             tcp_respond(tp, tp->t_template, (struct mbuf *)NULL,
  339.                 tp->rcv_nxt, tp->snd_una - 1, 0);
  340. #endif
  341.             tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
  342.         } else
  343.             tp->t_timer[TCPT_KEEP] = tcp_keepidle;
  344.         break;
  345.     dropit:
  346.         tcpstat.tcps_keepdrops++;
  347.         tp = tcp_drop(tp, ETIMEDOUT);
  348.         break;
  349.     }
  350.     return (tp);
  351. }
  352.